package net.tp.algo.text.sa;

import java.util.Arrays;

/**
 *
 * <p>Naive Suffix Array Builder.</p>
 *
 * <p>Example: ababc¥</p>
 * <pre>
 *     0 | ababc¥
 *     1 | babc¥
 *     2 | abc¥
 *     3 | bc¥
 *     4 | c¥
 *     5 | ¥
 *
 * </pre>
 *
 * @author Trung Phan
 *
 */
public class NaiveSA {

    public static int[] buildSA(String text) {

        String[] suffixes = new String[text.length()];
        for (int i = 0; i < suffixes.length; i++) {
            suffixes[i] = text.substring(i);
        }

        Arrays.sort(suffixes);

        int[] result = new int[text.length()];
        for (int i = 0; i < result.length; i++) {
            result[i] = text.length() - suffixes[i].length();
        }

        return result;
    }

}
